import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.mysql.jdbc.Statement;

public class SqlConnect {

	public static void main(String[] args) {

		Connection connection = null;

		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			connection = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/ksiazki", "root", "sklejka");
			System.out.println("Connection with database successful ...");

		} catch (ClassNotFoundException e) {
			System.out.println("Problen with database driver ...");
		} catch (SQLException e) {
			System.out.println("Unable to connect to database ...");
		} catch (Exception e) {
			System.out.println("Error with database connection ...");
		}

		try {
			Statement statement = (Statement) connection.createStatement();
			ResultSet resultSet = statement
					.executeQuery("SELECT * FROM ksiazki");
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String title = resultSet.getString("tytul");
				String author = resultSet.getString("autor");
				double price = resultSet.getDouble("cena");
				System.out.println(id + " " + author + " " + title + " "
						+ price);
			}
			resultSet.close();
		} catch (Exception e) {
			System.out.println("Error when downloading data ...");
		}

		if (connection != null) {
			try {
				connection.close();
				System.out.println("Connection close ...");
			} catch (Exception e) {
				System.out.println("Error when closing a database ...");
			}
		}

	}

}
